<?php


namespace App\Http\Controllers\Manage\System\Dictionary;


use App\Http\Controllers\Controller;
use App\Http\Requests\Manage\System\Dictionary\Currency\StoreRequest;
use App\Http\Requests\Manage\System\Dictionary\Currency\UpdateRequest;
use App\Http\Searchs\Manage\System\Dictionary\CurrencySearch;
use App\Repositories\Currency;

class CurrencyController extends Controller
{
    /**
     * @var Currency
     */
    private $currency;

    /**
     * CurrencyController constructor.
     * @param Currency $currency
     */
    public function __construct(Currency $currency)
    {
        $this->currency = $currency;
    }

    /**
     * @param CurrencySearch $search
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(CurrencySearch $search){
        return $this->response(
            $this->currency->paginate($search->toArray(), request()->input('per_page'))
        );
    }

    /**
     * @param StoreRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function store(StoreRequest $request){
        return $this->response(
            $this->currency->create($request->only(['code', 'name']))
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function edit($id){
        return $this->response(
            $this->currency->find($id)
        );
    }

    /**
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     * @author luffyzhao@vip.126.com
     */
    public function update(UpdateRequest $request, $id){
        return $this->response(
            $this->currency->update($id, $request->only(['name']))
        );
    }
}
